import { login } from '@api/login'
const user = {
  namespaced:true,  //开启命名空间模块
  state:{ // 状态
    token:'',
    userInfo:{}
  },
  mutations:{ // 计算属性
    SET_TOKEN:(state,token) => {
      state.token = token
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
    },
  },
  actions:{ // 异步操作
    Login(context, data){
      console.log('从login.vue传回来的数据', data)
      let params = {
        password:data.password.trim(),
        userName:data.username
      }
      return new Promise((resolve, reject) => {
        login(params).then(res => {
          context.commit('SET_TOKEN', `Bearer ${res.data.token}`)
          context.commit('SET_USERINFO', res.data)
          resolve(res.data)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}
export default user
